package com.haoshuang.zhouzhoubang.ui.page.study.exam.submit

import android.content.Context
import android.content.Intent
import android.os.Bundle
import com.haoshuang.zhouzhoubang.databinding.ActivitySubmitAnswerBinding
import com.haoshuang.zhouzhoubang.ui.page.study.exam.ExamActivity
import com.mufeng.libs.base.BaseVMActivity
import com.mufeng.libs.utils.ktx.formatMaxNum
import com.mufeng.libs.utils.ktx.formatTen
import com.mufeng.libs.utils.view.click
import com.mufeng.libs.utils.startPage
import com.mufeng.libs.utils.view.clickWithTrigger

/**
 *  提交答卷
 */
class SubmitAnswerActivity : BaseVMActivity<SubmitAnswerViewModel, ActivitySubmitAnswerBinding>() {

    companion object {
        fun launch(
            context: Context,
            total: Int,
            rightNum: Int,
            integral: Int,
            time: Int,
            articleId: Int
        ) {
            context.startPage<SubmitAnswerActivity>(
                "total" to total,
                "right" to rightNum,
                "integral" to integral,
                "time" to time,
                "article_id" to articleId
            )
        }
    }

    /**总题数*/
    private var total = 0

    /**正确数*/
    private var rightNum = 0

    /**积分*/
    private var integral = 0

    /**时间 妙级*/
    private var time = 0

    private var articleId = 0

    override fun initIntent() {
        total = intent.getIntExtra("total", 0)
        rightNum = intent.getIntExtra("right", 0)
        integral = intent.getIntExtra("integral", 0)
        time = intent.getIntExtra("time", 0)
        articleId = intent.getIntExtra("article_id", 0)
    }

    override fun initView(savedInstanceState: Bundle?) {
        binding.titleBar.title = "提交答案"
        binding.titleBar.leftImageView().click {
            setResult(200, Intent().apply {
                putExtra("isFinish", true)
            })
            finish()
        }

        binding.tvAgain.clickWithTrigger {
            setResult(200, Intent().apply { putExtra("isFinish", true) })
            finish()
            ExamActivity.launch(mActivity, 1, articleId)
        }

        binding.tvReturn.clickWithTrigger {
            setResult(200, Intent().apply { putExtra("isFinish", true) })
            finish()
        }

        binding.tvCorrectNumber.text = "$rightNum"
        binding.tvWrongNumber.text = "错题数：${total - rightNum}"
        binding.tvAccuracyRate.text = "正确率：${rightNum * 100 / total}%"
        binding.tvIntegration.text = "积分：+${integral}"
        binding.tvTimer.text = "用时：${getTimeStr()}"

    }

    /**
     * 格式化时间
     */
    private fun getTimeStr(): String {
        var h = 0
        var m = 0
        var s = 0
        if (time > 3600){
            h = time/3600
            time -= h * 3600
        }
        if (time > 60){
            m = time/60
            time -= m * 60
        }
        s = time
        var timeStr = ""
        timeStr = h.formatTen()
        timeStr += ":${m.formatTen()}"
        timeStr += ":${s.formatTen()}"
        return timeStr
    }

    override fun initData() {
    }

    override fun startObserve() {
    }

}